/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.uch.biblioteca.managedBeans;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;
import java.util.ArrayList;
import net.uch.biblioteca.hibernateSpringDao.HSBibLectorDAO;
import net.uch.biblioteca.mapping.BibLector;
import net.uch.biblioteca.mapping.TbCatalogo;
import net.uch.util.CommonDAO;
import javax.faces.component.UIParameter;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import java.util.Enumeration;
/**
 *
 * @author Vasquez
 */
public class mbLector {

    private String oncomplete;
    private String sLec_descripcion;
    private int iLec_id;
    private String sLec_paterno;
    private String sLec_materno;
    private String sLec_nombre;
    private String sLec_dni; // tamaño: 8
    private String sLec_telefono;
    private String sLec_correo;
    private String sLec_direccion;
    private String sLec_codigo = ""; //codigo con fecha en la que se incribe (05100701)
    private String sLec_password;
    private String sLec_tipo;
    private String sLec_activo = "1";
    private SelectItem[] cboLecTip;
    private SelectItem[] cboEstado;
    private String sLec_tip_id;
    private String sLec_tip_id_bus;
    private List<mbLector> lectores = new ArrayList<mbLector>();
    private String edit_disable;
    BibLector lec;

    //CONSTRUCTORES
    public mbLector () {
    }

    public mbLector ( int a ) {
    }

    

    

    //SETTER Y GETTER
    public String getsLec_tip_id_bus() {
        return sLec_tip_id_bus;
    }

    public void setsLec_tip_id_bus(String sLec_tip_id_bus) {
        this.sLec_tip_id_bus = sLec_tip_id_bus;
    }
    
    public BibLector getLec () {
        return lec;
    }

    public void setLec ( BibLector lec ) {
        this.lec = lec;
    }
    
    public SelectItem[] getCboEstado () {

        cboEstado = new SelectItem[ 2 ];
        cboEstado[0] = new SelectItem( "0", "INACTIVO" );
        cboEstado[1] = new SelectItem( "1", "ACTIVO" );

        return cboEstado;
    }

    public void setCboEstado ( SelectItem[] cboEstado ) {
        this.cboEstado = cboEstado;
    }

    public String getsLec_activo () {
        return sLec_activo;
    }

    public void setsLec_activo ( String sLec_activo ) {
        this.sLec_activo = sLec_activo;
    }

    public List<mbLector> getLectores () {
        return lectores;
    }

    public void setLectores ( List<mbLector> lectores ) {
        this.lectores = lectores;
    }

    public String getsLec_tip_id () {
        return sLec_tip_id;
    }

    public void setsLec_tip_id ( String sLec_tip_id ) {
        this.sLec_tip_id = sLec_tip_id;
    }

    public SelectItem[] getCboLecTip () {
        try {

            List lstLecTip = CommonDAO.getTbCatalogoDAO().seleccionarCatalogo( "003" );
            if ( lstLecTip.size() > 0 ) {
                cboLecTip = new SelectItem[ lstLecTip.size() + 1 ];
                cboLecTip[0] = new SelectItem( "0", "[Seleccione]" );
                for ( int i = 0; i < lstLecTip.size(); i++ ) {
                    cboLecTip[i + 1] = new SelectItem( ((TbCatalogo) lstLecTip.get( i )).getCatCodigoGrupo() + ((TbCatalogo) lstLecTip.get( i )).getCatCodigoElemento(), ((TbCatalogo) lstLecTip.get( i )).getCatDescripcionElemento() );
                }
            } else {
                cboLecTip = new SelectItem[ 1 ];
                cboLecTip[0] = new SelectItem( "0", "[Seleccione]" );
            }
        } catch ( Exception e ) {
            e.printStackTrace();
        }
        return cboLecTip;
    }

    public void setCboLecTip ( SelectItem[] cboLecTip ) {
        this.cboLecTip = cboLecTip;
    }

    public String getsLec_tipo () {
        return sLec_tipo;
    }

    public void setsLec_tipo ( String sLec_tipo ) {
        this.sLec_tipo = sLec_tipo;
    }

    public String getsLec_descripcion () {
        return sLec_descripcion;
    }

    public void setsLec_descripcion ( String sLec_descripcion ) {
        this.sLec_descripcion = sLec_descripcion;
    }

    public String getEdit_disable () {
        return edit_disable;
    }

    public void setEdit_disable ( String edit_disable ) {
        this.edit_disable = edit_disable;
    }

    public int getiLec_id () {
        return iLec_id;
    }

    public void setiLec_id ( int iLec_id ) {
        this.iLec_id = iLec_id;
    }

    public String getsLec_codigo () {
        return sLec_codigo;
    }

    public void setsLec_codigo ( String sLec_codigo ) {
        this.sLec_codigo = sLec_codigo;
    }

    public String getsLec_correo () {
        return sLec_correo;
    }

    public void setsLec_correo ( String sLec_correo ) {
        this.sLec_correo = sLec_correo;
    }

    public String getsLec_direccion () {
        return sLec_direccion;
    }

    public void setsLec_direccion ( String sLec_direccion ) {
        this.sLec_direccion = sLec_direccion;
    }

    public String getsLec_dni () {
        return sLec_dni;
    }

    public void setsLec_dni ( String sLec_dni ) {
        this.sLec_dni = sLec_dni;
    }

    public String getsLec_materno () {
        return sLec_materno;
    }

    public void setsLec_materno ( String sLec_materno ) {
        this.sLec_materno = sLec_materno;
    }

    public String getsLec_nombre () {
        return sLec_nombre;
    }

    public void setsLec_nombre ( String sLec_nombre ) {
        this.sLec_nombre = sLec_nombre;
    }

    public String getsLec_password () {
        return sLec_password;
    }

    public void setsLec_password ( String sLec_password ) {
        this.sLec_password = sLec_password;
    }

    public String getsLec_paterno () {
        return sLec_paterno;
    }

    public void setsLec_paterno ( String sLec_paterno ) {
        this.sLec_paterno = sLec_paterno;
    }

    public String getsLec_telefono () {
        return sLec_telefono;
    }

    public void setsLec_telefono ( String sLec_telefono ) {
        this.sLec_telefono = sLec_telefono;
    }

    public String getOncomplete () {
        return oncomplete;
    }

    public void setOncomplete ( String oncomplete ) {
        this.oncomplete = oncomplete;
    }

    public void nuevoLector ( ActionEvent event ) throws Exception {

        this.setiLec_id( 0 );
        this.setsLec_paterno( "" );
        this.setsLec_materno( "" );
        this.setsLec_nombre( "");
        this.setsLec_dni( "" );
        this.setsLec_telefono( "" );
        this.setsLec_correo( "" );
        this.setsLec_direccion( "" );
        this.setsLec_codigo( "" );
        this.setsLec_password( "" );
        this.setiLec_id( 0 );
        this.setOncomplete( "" );
        this.setEdit_disable( "false" );
        this.setOncomplete( "Richfaces.showModalPanel('mpLector')" );
    }

    public String seleccionarLector () throws Exception {
        HSBibLectorDAO daoLec = CommonDAO.getBibLectorDAO();
        List lstLector = daoLec.seleccionarLector( this.sLec_descripcion.trim(), this.sLec_tip_id_bus );
        this.lectores = new ArrayList<mbLector>();
        for ( int i = 0; i < lstLector.size(); i++ ) {
            mbLector lector = new mbLector( 0 );

            lector.setiLec_id( ((BibLector) lstLector.get( i )).getLecId() );
            lector.setsLec_paterno( ((BibLector) lstLector.get( i )).getLecPaterno() );
            lector.setsLec_materno( ((BibLector) lstLector.get( i )).getLecMaterno() );
            lector.setsLec_nombre( ((BibLector) lstLector.get( i )).getLecNombre() );
            lector.setsLec_dni( ((BibLector) lstLector.get( i )).getLecDni() );
            lector.setsLec_telefono( ((BibLector) lstLector.get( i )).getLecTelefono() );
            lector.setsLec_correo( ((BibLector) lstLector.get( i )).getLecCorreo() );
            lector.setsLec_direccion( ((BibLector) lstLector.get( i )).getLecDireccion() );
            lector.setsLec_codigo( ((BibLector) lstLector.get( i )).getLecCodigo() );
            lector.setsLec_password( ((BibLector) lstLector.get( i )).getLecPassword() );
            lector.setsLec_tip_id( ((BibLector) lstLector.get( i )).getLecTip() );
            lector.setsLec_activo( ((BibLector) lstLector.get( i )).getLecActivo().toString() );

            //lector.setVer( true );
            //lector.setEditar( false );
            lectores.add( lector );

        }
        return "";
    }

    public void guardarLector ( ActionEvent event ) throws Exception {
        this.setOncomplete( "" );

        HSBibLectorDAO daoLec = CommonDAO.getBibLectorDAO();
        if ( this.getsLec_paterno().trim().length() > 0 && this.getsLec_materno().trim().length() > 0
                && this.getsLec_nombre().trim().length() > 0 && this.getsLec_dni().trim().length() > 0
                && this.getsLec_direccion().trim().length() > 0 && this.getsLec_codigo().trim().length() >0
                && this.getsLec_password().trim().length() > 0
                && !"0".equals( this.getsLec_tipo() ) ) {

            if ( this.getiLec_id() > 0 ) { // ACTUALIZAR LECTOR

                BibLector lec = new BibLector();

                lec.setLecId( this.getiLec_id() );
                lec.setLecPaterno( this.getsLec_paterno() );
                lec.setLecMaterno( this.getsLec_materno() );
                lec.setLecNombre( this.getsLec_nombre() );
                lec.setLecDni( this.getsLec_dni() );
                lec.setLecTelefono( this.getsLec_telefono() );
                lec.setLecCorreo( this.getsLec_correo() );
                lec.setLecDireccion( this.getsLec_direccion() );
                lec.setLecCodigo( this.getsLec_codigo() );
                lec.setLecPassword( this.getsLec_password() );
                lec.setLecTip( this.getsLec_tip_id() );
                lec.setLecActivo( this.getsLec_activo().charAt( 0) );

                daoLec.actualizarLector( lec );
                this.setOncomplete( "javascript:alert('Actualizacion Satisfactorio.');Richfaces.hideModalPanel('mpLector')" );
            } else { // NUEVO LECTOR

                BibLector lec = new BibLector();

                lec.setLecId( this.getiLec_id() );
                lec.setLecPaterno( this.getsLec_paterno() );
                lec.setLecMaterno( this.getsLec_materno() );
                lec.setLecNombre( this.getsLec_nombre() );
                lec.setLecDni( this.getsLec_dni() );
                lec.setLecTelefono( this.getsLec_telefono() );
                lec.setLecCorreo( this.getsLec_correo() );
                lec.setLecDireccion( this.getsLec_direccion() );
                lec.setLecCodigo( this.getsLec_codigo() );
                lec.setLecPassword( this.getsLec_password() );
                lec.setLecTip( this.getsLec_tip_id() );
                lec.setLecActivo( this.getsLec_activo().charAt(0) );

                daoLec.guardarLector( lec );
                this.setOncomplete( "javascript:alert('Registro Satisfactorio.');Richfaces.hideModalPanel('mpLector')" );
            }
        } else {
            this.setOncomplete( "javascript:alert('Debe ingresar todos los Datos.')" );
        }
    }

    public void eliminarLector ( ActionEvent event ) throws Exception {
        
        this.setOncomplete( "" );
        HSBibLectorDAO daoLec = CommonDAO.getBibLectorDAO();
        this.setOncomplete( "javascript:alert('El Lector se elimino correctamente')" );
        daoLec.eliminarLector( this.getiLec_id() );


    }

    public void actualizarLector ( ActionEvent event ) throws Exception {
        this.setOncomplete( "" );
        HSBibLectorDAO daoUsu = CommonDAO.getBibLectorDAO();
        int id_update = Integer.parseInt( ((UIParameter) event.getComponent().findComponent( "id_update" )).getValue().toString() );
        BibLector lec = daoUsu.buscarLector( id_update );
        this.setEdit_disable( "true" );

        this.setiLec_id( lec.getLecId() );
        this.setsLec_paterno( lec.getLecPaterno() );
        this.setsLec_materno( lec.getLecMaterno() );
        this.setsLec_nombre( lec.getLecNombre() );
        this.setsLec_dni( lec.getLecDni() );
        this.setsLec_telefono( lec.getLecTelefono() );
        this.setsLec_correo( lec.getLecCorreo() );
        this.setsLec_direccion( lec.getLecDireccion() );
        this.setsLec_codigo( lec.getLecCodigo() );
        this.setsLec_password( lec.getLecPassword() );
        this.setsLec_tip_id( lec.getLecTip() );
        this.setsLec_activo( lec.getLecActivo().toString() );


        this.setOncomplete( "Richfaces.showModalPanel('mpLector')" );

    }

    public void verificarEliminacion ( ActionEvent event ) {

        int id_delete = Integer.parseInt( ((UIParameter) event.getComponent().findComponent( "id_delete" )).getValue().toString() );
        this.setiLec_id( id_delete );
        this.setOncomplete( "javascript:Richfaces.showModalPanel('mpEliminar');" );

    }
    
    public void verificarLector ( ActionEvent event) throws SQLException, IOException {

        HSBibLectorDAO lecDao = CommonDAO.getBibLectorDAO();
        if ( (lec = lecDao.validarLector( this.sLec_codigo, this.sLec_password )) != null ) {

            ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
            HttpSession session = (HttpSession) context.getSession( false );
            session.setAttribute( "lector", lec );
            
           
        }
    }
    
    public String cerrar() {
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);

        Enumeration atributos = session.getAttributeNames();
        while (atributos.hasMoreElements()) {
            session.removeAttribute((String) atributos.nextElement());
        }
        session.invalidate();

        return "BusquedaReservaLec";
    }
}
